<?php
namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Debug\Debug;
use Zend\Filter\File\Rename;
use Zend\Session\Container;
use Zend\Mvc\MvcEvent;

class StatisticController extends AbstractActionController{

	public $view;
	public $session;
    protected $em;
    protected $authService;

	public function __construct(){
		$this->view = new ViewModel();
		$this->session = new Container();
        $this->em = $this->getEventManager();
        $this->em->attach(MvcEvent::EVENT_DISPATCH, array($this, 'preDispatch'),100);
		echo "<script> var controllername='statistic'; </script>";
	}

    public function preDispatch(){
        if(!$this->getAuthService()->hasIdentity())
            $this->_redirect()->toRoute(NULL,array('controller'=>'auth','action'=>'login'));
        $username = $this->getAuthService()->getStorage()->read();
        $this->layout('layout/admin');
        $this->layout()->username = $username;
    }

    public function getAuthService(){
        if(!$this->authService){
            $this->authService = $this->getServiceLocator()->get('AuthService');
        }
        return $this->authService;
    }

	public function indexAction(){
		$this->layout('layout/admin');
        $billTable = $this->getServiceLocator()->get('Admin\Model\BillTable');
        if($this->session->offsetExists('thongbao')){
            $this->view->thongbao = $this->session->offsetGet('thongbao');
            $this->session->offsetUnset('thongbao');
        }
        $arrParams = $this->getRequest()->getPost()->toArray();
        if($arrParams!= NULL){
        	$from = $arrParams['from'];
        	$to = $arrParams['to'];
        	$dateFormat = date_format(MvtDate,'%d-%m-%Y');
        }else{
        	// chưa submit nên lấy thông tin doanh thu trong tháng đầu tiên
        	$bills = $billTable->fetchAll();
        	$this->view->bills = $bills;
        }
        return $this->view;
	}
}